﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using System.Collections.ObjectModel;

namespace AutoSubmit.Lib.Articlecompilation
{
    public class Articlecompilation : ActionPage
    {
        public override void vCheckErrorBox()
        {
            IWebElement errBox = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_errBoxXPath), 5);
            if (errBox != null && errBox.Text.ToLower().Contains("sorry"))
            {
                throw new Exception(errBox.Text);
            }
        }

        public override String vGetArticleURL()
        {
            if (m_status != SubmitStatus.SUCCESS)
            {
                return "";
            }

            if (m_article.m_url.Length > 0)
            {
                m_driver.Navigate().GoToUrl(m_article.m_url);
            }

            // find new article
            ReadOnlyCollection<IWebElement> hrefs = WebDriverExtension.FindElements(m_driver, By.XPath(m_article.m_articlesListXPath), 5);
            foreach (IWebElement ele in hrefs)
            {
                if (ele.Text.Contains(m_article.m_title))
                    return ele.FindElement(By.XPath("./td[2]/a")).GetAttribute("href");
            }

            return "";
        }
    }
}
